package com.alaia.alaiacommoncore.auth.model;

import com.alaia.alaiacommoncore.model.SysUser;
import org.springframework.security.core.CredentialsContainer;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.time.LocalDateTime;
import java.util.Collection;
import java.util.Objects;

public class LoginAppUser implements UserDetails, CredentialsContainer {
    /**
     * 用户
     */
    private SysUser user;
    /**
     * 登录ip
     */
    private String loginIp;
    /**
     * 登录时间
     */
    private LocalDateTime loginTime;
    /**
     * 登陆类型
     */
    private Integer loginType;

    public LoginAppUser() {
    }

    public LoginAppUser(SysUser user, String loginIp, LocalDateTime loginTime, Integer loginType) {
        this.user = user;
        this.loginIp = loginIp;
        this.loginTime = loginTime;
        this.loginType = loginType;
    }
    public LoginAppUser(SysUser user, String loginIp, LocalDateTime loginTime, String loginType) {
        this.user = user;
        this.loginIp = loginIp;
        this.loginTime = loginTime;
        this.loginType = Integer.valueOf(loginType);
    }
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return null;
    }
    @Override
    public String getPassword() {
        return user.getPassword();
    }
    @Override
    public String getUsername() {
        return user.getUserName();
    }
    /**
     * 账户是否未过期，过期无法验证
     */
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }
    /**
     * 指定用户是否解锁，锁定的用户无法进行身份验证
     * <p>
     * 密码锁定
     * </p>
     */
    @Override
    public boolean isAccountNonLocked() {
        return true;
    }
    /**
     * 指示是否已过期的用户的凭据(密码)，过期的凭据防止认证
     */
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }
    /**
     * 用户是否被启用或禁用。禁用的用户无法进行身份验证。
     */
    @Override
    public boolean isEnabled() {
        return true;
    }
    /**
     * 认证完成后，擦除密码
     */
    @Override
    public void eraseCredentials() {
        user.setPassword(null);
    }

    public SysUser getUser() {
        return user;
    }

    public void setUser(SysUser user) {
        this.user = user;
    }

    public String getLoginIp() {
        return loginIp;
    }

    public void setLoginIp(String loginIp) {
        this.loginIp = loginIp;
    }

    public LocalDateTime getLoginTime() {
        return loginTime;
    }

    public void setLoginTime(LocalDateTime loginTime) {
        this.loginTime = loginTime;
    }

    public Integer getLoginType() {
        return loginType;
    }

    public void setLoginType(Integer loginType) {
        this.loginType = loginType;
    }
    //这里为什么要重写equals和hashcode
    //因为Spring Security 中通过 SessionRegistryImpl 类来实现对会话信息的统一管理
    //在初始化对象的时候SessionRegistryImpl回初始化一个集合：private final ConcurrentMap<Object, Set<String>> principals;
    //他使用对象作为key，所以在寻找登陆用户的时候，会找不到
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        LoginAppUser hr = (LoginAppUser) o;
        return Objects.equals(this.getUser().getUserName(), hr.getUser().getUserName());
    }

    @Override
    public int hashCode() {
        return Objects.hash(this.getUser().getUserName());
    }
}
